package com.itheima.mall.controller;


import com.itheima.mall.pojo.ProductAttribute;
import com.itheima.mall.pojo.ProductCategoryAttributeRelation;
import com.itheima.mall.service.PmsProductAttributeService;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.ProductAttrInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Ref;
import java.util.List;

@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;
    //根据商品分类的id获取商品属性及属性分类

    @GetMapping("/attrInfo/{productCategoryId}")
    public ResponseEntity selectByproductCategoryId(@PathVariable("productCategoryId") Integer productCategoryId){
       List<ProductCategoryAttributeRelation> infos = pmsProductAttributeService.selectByproductCategoryId(productCategoryId);
       return ResponseEntity.ok(infos);
    }

    //添加商品属性信息
    @PostMapping("/create")
    public ResponseEntity createProductAttribute(@RequestBody ProductAttribute productAttributeParam ){
        pmsProductAttributeService.createProductAttribute(productAttributeParam);
        return ResponseEntity.ok("");
    }
    //删除商品属性信息
    @PostMapping("/delete")
    public ResponseEntity deleteProductAttributeByIds(@RequestParam("ids") Integer[] ids){
        pmsProductAttributeService.deleteProductAttributeByIds(ids);
        return ResponseEntity.ok("");
    }

    //修改商品属性信息
    @PostMapping("/update/{id}")
    public ResponseEntity updateProductAttributeById(@PathVariable("id") Integer id ,@RequestBody ProductAttribute productAttribute){

        pmsProductAttributeService.updateProductAttributeById(id ,productAttribute);

        return ResponseEntity.ok("");
    }
    //查询单个商品属性信息
    @GetMapping("/{id}")
    public ResponseEntity selectProductAttributeById(@PathVariable("id") Integer id){

        pmsProductAttributeService.selectProductAttributeById(id);
        return ResponseEntity.ok("");
    }
    //根据分类查询属性列表或参数列表
    @GetMapping("/list/{cid}")
    public ResponseEntity selectPageByCidAndType(@PathVariable("cid") Integer cid ,
                                                  @RequestParam("type") Long type ,
                                                  @RequestParam(defaultValue = "1" ) Integer pageNum ,
                                                  @RequestParam(defaultValue = "10" ) Integer pageSize){
        PageResult page = pmsProductAttributeService.selectPageByCidAndType(cid , type , pageNum , pageSize);
        return ResponseEntity.ok(page);
    }

}
